package com.zhangbo.share.baffle;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.zhangbo.share.bean.BaffleRule;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author zhangbo
 * @version 1.00
 * @classdesc <pre></pre>
 * @create 2021/12/23 10:16
 * @update <pre>
 *     版本           修改人         修改日期         修改内容描述
 *     -----------------------------------------------------------
 *     1.00           zhangbo     2021/12/23 10:16
 *     -----------------------------------------------------------
 * </pre>
 */
public class DataBaffleUtil {

    public static final DateTimeFormatter YMD = DateTimeFormatter.ofPattern("yyyyMMdd");

    public static final DateTimeFormatter YMDHMS = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");


    /**
     * 生成id类型
     *
     * @param baffleRule
     * @return
     */
    public static final String generateId(BaffleRule baffleRule) {
        String randStr = null;
        if (baffleRule.getIdLength() > 14) {
            randStr = YMDHMS.format(LocalDateTime.now()) + RandomUtil.randomNumbers(baffleRule.getIdLength() - 14);
        } else if (baffleRule.getIdLength() > 8) {
            randStr = YMD.format(LocalDateTime.now()) + RandomUtil.randomNumbers(baffleRule.getIdLength() - 8);
        } else {
            randStr = RandomUtil.randomNumbers(baffleRule.getIdLength());
        }
        return baffleRule.getIdPrefix() + randStr;
    }

    /**
     * 生成入参类型
     *
     * @param param
     * @param baffleRule
     * @return
     */
    public static final Object generateParam(JSONObject param, BaffleRule baffleRule) {
        String[] paramNames = baffleRule.getReturnParamName().split("\\.");
        JSONObject current = param;
        for (int i = 0; i < paramNames.length; i++) {
            if (current.containsKey(paramNames[i])) {
                if (paramNames.length == i + 1) {
                    return current.get(paramNames[i]);
                } else {
                    current = current.getJSONObject(paramNames[i]);
                }
            } else {
                return null;
            }
        }
        return null;
    }

    /**
     * 生成枚举值
     *
     * @param baffleRule
     * @return
     */
    public static final String generateEnum(BaffleRule baffleRule) {
        List<String> enumValues = baffleRule.getEnumValues();
        if (enumValues.size() > 1) {
            int sequence = RandomUtil.randomInt(0, enumValues.size());
            return enumValues.get(sequence);
        } else if (enumValues.size() == 1) {
            return enumValues.get(0);
        }
        return null;
    }

    /**
     * 生成日期
     *
     * @param baffleRule
     * @return
     */
    public static final String generateDate(BaffleRule baffleRule) {
        LocalDateTime now = LocalDateTime.now();
        if (DataBaffleEnums.BaffleDateAddTypeEnum.DAY.getValue().equals(baffleRule.getDateAddType())) {
            now = now.plusDays(baffleRule.getDateIncrement());
        } else if (DataBaffleEnums.BaffleDateAddTypeEnum.MONTH.getValue().equals(baffleRule.getDateAddType())) {
            now = now.plusMonths(baffleRule.getDateIncrement());
        } else if (DataBaffleEnums.BaffleDateAddTypeEnum.YEAR.getValue().equals(baffleRule.getDateAddType())) {
            now = now.plusYears(baffleRule.getDateIncrement());
        }
        return DateTimeFormatter.ofPattern(baffleRule.getBaseDate()).format(now);
    }


}
